<?php

namespace app\admin\controller\wanlshop;

use app\admin\model\wanlshop\AddressSpot;
use app\admin\model\wanlshop\Industy;
use app\api\controller\wanlshop\Separate;
use app\common\controller\Backend;
use think\Db;
use think\Exception;

use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 认证管理
 *
 * @icon fa fa-circle-o
 */
class Bill extends Backend
{

    /**
     * Auth模型对象
     * @var \app\admin\model\wanlshop\Auth
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\wanlshop\Order;

    }

    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(null,'order');
            $total = $this->model
                ->alias('order')
                ->with(['shop','spot','pay','ordergoods'])
                ->whereIn('order.state',[2,3,4,6,9])
                ->where('order.is_refund',1)
                ->where('order.is_integral',0)
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->alias('order')
                ->with(['shop','spot','pay','ordergoods'])
                ->whereIn('order.state',[2,3,4,6,9])
                ->where('order.is_integral',0)
                ->where('order.is_refund',1)
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $serviceCharge = 0;
            $platform_rake_all = 0;
            $pick_up_rake_all = 0;
            $income_price_all = 0;
            $spot_unsettled = 0;
            $shop_unsettled = 0;
            $platform_unsettled = 0;
            foreach ($list as $row) {
                $platform_rake = 0;//平台抽成金额
                $pick_up_rake = 0;//自提点抽成金额
                $score_price = $row['pay']['score_price']; //积分抵扣金额
                //算手续费,支付金额的千分之6 最低取0.01
                // 支付金额
                $payPrice = $row['pay']['price'];
                // 计算手续费，支付金额的千分之六
                $charge = $payPrice * 0.006;
                // 确保手续费不低于0.01元
                if ($charge < 0.01) {
                    $charge = 0.01;
                }
                $row['charge'] = $charge;
                // 累加手续费
                $serviceCharge += $charge;
                $pick_up = AddressSpot::where('type',1)->find($row['spot_id']);
                foreach ($row['ordergoods'] as $ordergood){
                    if(!empty($ordergood['take_proportion'])){
                        $platform_rake = bcadd($platform_rake,$ordergood['take_proportion']/100*$ordergood['price'],2);
                    }
                    if(!empty($ordergood['sharing_proportion']) && $pick_up){
                        $pick_up_rake = bcadd($pick_up_rake,$ordergood['sharing_proportion']/100*$ordergood['price'],2);
                    }
                }
                if($score_price > 0){//扣除订单总抵扣金额
                    $platform_rake = bcsub($platform_rake,$score_price,2);
                }
                $row['platform_rake'] = $platform_rake;
                $row['pick_up_rake'] = $pick_up_rake;
                $income = bcsub($payPrice, $charge, 2);
                $income = bcsub($income, $platform_rake, 2);
                $row['income_price'] = bcsub($income, $pick_up_rake, 2);
                if($row['is_commission'] == 0){
                    $platform_unsettled = bcadd($platform_unsettled, $platform_rake, 2);
                    $spot_unsettled= bcadd($spot_unsettled, $pick_up_rake, 2);
                    $shop_unsettled  = bcadd($shop_unsettled, $row['income_price'], 2);
                }else{
                    $platform_rake_all = bcadd($platform_rake_all, $platform_rake, 2);
                    $pick_up_rake_all = bcadd($pick_up_rake_all, $pick_up_rake, 2);
                    $income_price_all = bcadd($income_price_all, $row['income_price'], 2);
                }
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list,
                    'platform_rake_all'=>$platform_rake_all,
                    'pick_up_rake_all'=>$pick_up_rake_all,
                    'income_price_all'=>$income_price_all,
                    'platform_unsettled'=>$platform_unsettled,
                    'shop_unsettled'=>$shop_unsettled,
                    'spot_unsettled'=>$spot_unsettled
            );

            return json($result);
        }
        return $this->view->fetch();
    }
}
